import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Render } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';
import { CreateUserExtendDto } from '../userExtend/userExtend.dto';
import { EntityManager, Transaction, TransactionManager } from 'typeorm';
import { CreateUserDto } from './dto/create.user.dto';
import { AddRoleDto } from './dto/add.role.dto';
import { NestLogService } from '@juliana_mewo/nest-log';


@Controller('user')
export class UserController {
    // 使用方式需要使用@Inject的方式
    constructor (
      private readonly userService: UserService,
      private readonly nestLogService: NestLogService,
    ) { }

    @Post()
    @Transaction()
    async create(@Body() data: Extract<CreateUserDto, CreateUserExtendDto>, @TransactionManager() manager: EntityManager,) {
        return this.userService.create(data, manager);
    }

    @Get()
    async userList(): Promise<User[]> {
        this.nestLogService.log('获取用户信息')
        console.log('log test middlewares');
        return await this.userService.userList();

        // console.log('获取用户数据');
        // throw new HttpException('获取数据错误', HttpStatus.OK);
    }

    @Delete(':id')
    async del(@Param('id', new ParseIntPipe()) id: number) {
        return await this.userService.delete(id);
    }

    @Post('/addRole')
    @Transaction()
    async addRole(@Body() data: AddRoleDto, @TransactionManager() manager: EntityManager ){
        return this.userService.addRole(data)
    }

    @Get('login')
    @Render('login') // 渲染views里面的ejs模板
    loginPage() {
        return { 'title': '登录页面' } // 返回给ejs模板的数据
    }

}
